package com.example.fw;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase{
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	private SortedListOf<ContactData> cachedContacts;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null)
			rebuildContacts();
		return cachedContacts;
		
	}

	private void rebuildContacts() {
		manager.navigateTo().homePage();
		cachedContacts = new SortedListOf<ContactData>();
		List<WebElement> firstNames = driver.findElements(By.xpath("//tr[@name='entry']/td[3]"));
		List<WebElement> lastNames = driver.findElements(By.xpath("//tr[@name='entry']/td[2]"));
		List<WebElement> checkboxes = driver.findElements(By.xpath("//input[@name='selected[]']"));
		List<WebElement> emails = driver.findElements(By.xpath("//tr[@name='entry']/td[4]/a"));
		List<WebElement> phones = driver.findElements(By.xpath("//tr[@name='entry']/td[5]"));
		int i=0;
		for (WebElement element : firstNames) {
			String firstName = element.getText();
			String lastName = lastNames.get(i).getText();
			int id = Integer.parseInt(checkboxes.get(i).getAttribute("value"));
			String email = emails.get(i).getText();
			String homePhone = phones.get(i).getText();
			ContactData contact= new ContactData()
									.withFirstName(firstName)
									.withLastName(lastName)
									.withContactId(id)
									.withEmail(email)
									.withHomePhone(homePhone);
			cachedContacts.add(contact);
			i++;
		}			
	}

	public List<String> getGroups() {
		List<String> groups = new ArrayList<String>();
		List<WebElement> elements = driver.findElements(By.xpath("//select[@name='new_group']/option"));
		for (WebElement element : elements) {
			String group = element.getText();
			groups.add(group);
		}
		return groups;
	}

	
	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().homePage();
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomepage();	
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		manager.navigateTo().homePage();
		initContactModification(index);		
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomepage();
		return this;
	}
	
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().homePage();
		initContactModification(index);
		submitContactDeletion();
		returnToHomepage();
		return this;
	}

//-------------------------------------------------------------------------	
	
	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	}

	public void returnToHomepage() {
		click(By.linkText("home page"));
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHomePhone());
		type(By.name("mobile"), contact.getMobilePhone());
		type(By.name("work"), contact.getWorkPhone());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getAdditionalEmail());
		type(By.name("byear"), contact.getByear());
		type(By.name("address2"), contact.getSecondaryAddress());
		type(By.name("phone2"), contact.getSecondaryPhone());
		selectByText(By.name("bday"), contact.getBday());
		selectByIndex(By.name("bmonth"), contact.getBmonth());
		if (formType == CREATION){
			List<String> groups = getGroups();
			Random rnd = new Random();
			int index = rnd.nextInt(groups.size()-1);
			contact.withGroup(groups.get(index));
			selectByText(By.name("new_group"), contact.getGroup());
		}
		//else{
		//	if(driver.findElements(By.name("new_group")).size()!=0)
		//		throw new Error("Contact modification form contains group selector");
		//}
		return this;
	}

	public ContactHelper initContactModification(int index) {
		click(By.xpath("//tr[@name='entry'][" + (index + 1) + "]/descendant::img[@title='Edit']"));	
		return this;
	}

	public ContactHelper submitContactDeletion() {
		click(By.xpath("//input[@value='Delete']"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//input[@value='Update']"));
		cachedContacts = null;
		return this;
	}

	

	


}
